package www.wen.com.common.lib.base.fragment

import android.view.View
import android.widget.EditText
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.lifecycleScope
import com.lihang.ShadowLayout
import www.wen.com.common.R
import www.wen.com.common.lib.base.module.BaseCountDownModel
import www.wen.com.common.lib.data.entities.RequestVerifyBody
import www.wen.com.common.lib.extension.*
import www.wen.com.common.lib.util.SpUtil

/**
 * @author WEN
 * @Description:接收短信的基类,主要是处理有发送短信需要等待60秒这个共同的逻辑
 * @date 2020//25 17:48
 */
abstract class BaseReceiveSMSFragment<K : ViewDataBinding> : BaseLoadingFragment<K>(),
    View.OnClickListener {

    private lateinit var slVerificationCode: ShadowLayout

    private lateinit var baseCountDownModel: BaseCountDownModel


    override fun lazyInitView() {
        baseCountDownModel = viewModel as BaseCountDownModel
        slVerificationCode = dataBinding.root.findViewById(R.id.slVerificationCode)
        val remainderTime = SpUtil.decodeLong(getRecordTimeKey())!!
        val nowTime = System.currentTimeMillis()
        val difference = ((nowTime - SpUtil.decodeLong(getSystemRecordTimeKey())!!) / 1000)
        val realRemainderTime = remainderTime - difference
        if (realRemainderTime in 0..60) {
            initializeInCountBackwards()
            countDownCoroutines(
                total = realRemainderTime.toInt(),
                onTick = { tick ->
                    countdownIng(tick)
                    baseCountDownModel.mTiming.value = tick
                    baseCountDownModel.mVerificationCodeText.value = tick.verificationText()
                },
                onFinish = {
                    baseCountDownModel.run {
                        mEnabled.value = enableSendVerificationCode()
                    }
                },
                onCancel = {},
                scope = lifecycleScope
            )
        } else {
            uncalibratedTime()
        }
    }

    /**
     * 读秒中
     */
    open fun countdownIng(it: Int) {}

    /**
     * 未计算时间的初始化
     */
    open fun uncalibratedTime() {}

    /**
     * 计算时间的初始化
     */
    open fun initializeInCountBackwards() {}

    /**
     * 返回记录时间的key,一个倒计时对应不同的key
     */
    abstract fun getRecordTimeKey(): String

    /**
     * 返回记录时间的key,一个倒计时对应不同的key
     */
    abstract fun getSystemRecordTimeKey(): String


    override fun onDestroy() {
        super.onDestroy()
        SpUtil.encode(getRecordTimeKey(), baseCountDownModel.mTiming.value)
        SpUtil.encode(getSystemRecordTimeKey(), System.currentTimeMillis())
    }

    /**
     * 发送验证码
     */
    fun sendVerificationCode(
        tvPhoneNumber: EditText,
        etVerificationCode: EditText,
        itac: Int,
        baseCountDownModel: BaseCountDownModel
    ) {
        tvPhoneNumber.trimProxy().condition(
            condition = {
                true
            },
            conform = {
                baseCountDownModel.requestVerifyCode(RequestVerifyBody("+$itac", it)) {
                    etVerificationCode.requestFocus()
                    countDownCoroutines(
                        total = 60,
                        onTick = { tick ->
                            baseCountDownModel.run {
                                mTiming.value = tick
                                mVerificationCodeText.value = tick.verificationText()
                                mEnabled.value = enableSendVerificationCode()
                            }
                        },
                        scope = lifecycleScope
                    )
                }
            },
            incompatible = {
                toast(getString(R.string.phone_format_does_not_match))
            })
    }

}



